{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 15.13 \u4f20\u9012NULL\u7ed3\u5c3e\u7684\u5b57\u7b26\u4e32\u7ed9C\u51fd\u6570\u5e93\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u8981\u5199\u4e00\u4e2a\u6269\u5c55\u6a21\u5757\uff0c\u9700\u8981\u4f20\u9012\u4e00\u4e2aNULL\u7ed3\u5c3e\u7684\u5b57\u7b26\u4e32\u7ed9C\u51fd\u6570\u5e93\u3002\n\u4e0d\u8fc7\uff0c\u4f60\u4e0d\u662f\u5f88\u786e\u5b9a\u600e\u6837\u4f7f\u7528Python\u7684Unicode\u5b57\u7b26\u4e32\u53bb\u5b9e\u73b0\u5b83\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8bb8\u591aC\u51fd\u6570\u5e93\u5305\u542b\u4e00\u4e9b\u64cd\u4f5cNULL\u7ed3\u5c3e\u7684\u5b57\u7b26\u4e32\uff0c\u88ab\u58f0\u660e\u7c7b\u578b\u4e3a char * .\n\u8003\u8651\u5982\u4e0b\u7684C\u51fd\u6570\uff0c\u6211\u4eec\u7528\u6765\u505a\u6f14\u793a\u548c\u6d4b\u8bd5\u7528\u7684\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "void print_chars(char *s) {\n    while (*s) {\n        printf(\"%2x \", (unsigned char) *s);\n\n        s++;\n    }\n    printf(\"\\n\");\n}"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6b64\u51fd\u6570\u4f1a\u6253\u5370\u88ab\u4f20\u8fdb\u6765\u5b57\u7b26\u4e32\u7684\u6bcf\u4e2a\u5b57\u7b26\u7684\u5341\u516d\u8fdb\u5236\u8868\u793a\uff0c\u8fd9\u6837\u7684\u8bdd\u53ef\u4ee5\u5f88\u5bb9\u6613\u7684\u8fdb\u884c\u8c03\u8bd5\u4e86\u3002\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "print_chars(\"Hello\");   // Outputs: 48 65 6c 6c 6f"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5bf9\u4e8e\u5728Python\u4e2d\u8c03\u7528\u8fd9\u6837\u7684C\u51fd\u6570\uff0c\u4f60\u6709\u51e0\u79cd\u9009\u62e9\u3002\n\u9996\u5148\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u8c03\u7528 PyArg_ParseTuple() \u5e76\u6307\u5b9a\u201dy\u201c\u8f6c\u6362\u7801\u6765\u9650\u5236\u5b83\u53ea\u80fd\u64cd\u4f5c\u5b57\u8282\uff0c\u5982\u4e0b\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "static PyObject *py_print_chars(PyObject *self, PyObject *args) {\n  char *s;\n\n  if (!PyArg_ParseTuple(args, \"y\", &s)) {\n    return NULL;\n  }\n  print_chars(s);\n  Py_RETURN_NONE;\n}"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u7ed3\u679c\u51fd\u6570\u7684\u4f7f\u7528\u65b9\u6cd5\u5982\u4e0b\u3002\u4ed4\u7ec6\u89c2\u5bdf\u5d4c\u5165\u4e86NULL\u5b57\u8282\u7684\u5b57\u7b26\u4e32\u4ee5\u53caUnicode\u652f\u6301\u662f\u600e\u6837\u88ab\u62d2\u7edd\u7684\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "print_chars(b'Hello World')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "print_chars(b'Hello\\x00World')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "print_chars('Hello World')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u4f60\u60f3\u4f20\u9012Unicode\u5b57\u7b26\u4e32\uff0c\u5728 PyArg_ParseTuple() \u4e2d\u4f7f\u7528\u201ds\u201c\u683c\u5f0f\u7801\uff0c\u5982\u4e0b\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "static PyObject *py_print_chars(PyObject *self, PyObject *args) {\n  char *s;\n\n  if (!PyArg_ParseTuple(args, \"s\", &s)) {\n    return NULL;\n  }\n  print_chars(s);\n  Py_RETURN_NONE;\n}"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5f53\u88ab\u4f7f\u7528\u7684\u65f6\u5019\uff0c\u5b83\u4f1a\u81ea\u52a8\u5c06\u6240\u6709\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u4ee5NULL\u7ed3\u5c3e\u7684UTF-8\u7f16\u7801\u3002\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "print_chars('Hello World')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "print_chars('Spicy Jalape\\u00f1o')  # Note: UTF-8 encoding"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "print_chars('Hello\\x00World')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "print_chars(b'Hello World')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u56e0\u4e3a\u67d0\u4e9b\u539f\u56e0\uff0c\u4f60\u8981\u76f4\u63a5\u4f7f\u7528 PyObject * \u800c\u4e0d\u80fd\u4f7f\u7528 PyArg_ParseTuple() \uff0c\n\u4e0b\u9762\u7684\u4f8b\u5b50\u5411\u4f60\u5c55\u793a\u4e86\u600e\u6837\u4ece\u5b57\u8282\u548c\u5b57\u7b26\u4e32\u5bf9\u8c61\u4e2d\u68c0\u67e5\u548c\u63d0\u53d6\u4e00\u4e2a\u5408\u9002\u7684 char * \u5f15\u7528\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "/* Some Python Object (obtained somehow) */\nPyObject *obj;\n\n/* Conversion from bytes */\n{\n   char *s;\n   s = PyBytes_AsString(o);\n   if (!s) {\n      return NULL;   /* TypeError already raised */\n   }\n   print_chars(s);\n}\n\n/* Conversion to UTF-8 bytes from a string */\n{\n   PyObject *bytes;\n   char *s;\n   if (!PyUnicode_Check(obj)) {\n       PyErr_SetString(PyExc_TypeError, \"Expected string\");\n       return NULL;\n   }\n   bytes = PyUnicode_AsUTF8String(obj);\n   s = PyBytes_AsString(bytes);\n   print_chars(s);\n   Py_DECREF(bytes);\n}"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u524d\u9762\u4e24\u79cd\u8f6c\u6362\u90fd\u53ef\u4ee5\u786e\u4fdd\u662fNULL\u7ed3\u5c3e\u7684\u6570\u636e\uff0c\n\u4f46\u662f\u5b83\u4eec\u5e76\u4e0d\u68c0\u67e5\u5b57\u7b26\u4e32\u4e2d\u95f4\u662f\u5426\u5d4c\u5165\u4e86NULL\u5b57\u8282\u3002\n\u56e0\u6b64\uff0c\u5982\u679c\u8fd9\u4e2a\u5f88\u91cd\u8981\u7684\u8bdd\uff0c\u90a3\u4f60\u9700\u8981\u81ea\u5df1\u53bb\u505a\u68c0\u67e5\u4e86\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u53ef\u80fd\u7684\u8bdd\uff0c\u4f60\u5e94\u8be5\u907f\u514d\u53bb\u5199\u4e00\u4e9b\u4f9d\u8d56\u4e8eNULL\u7ed3\u5c3e\u7684\u5b57\u7b26\u4e32\uff0c\u56e0\u4e3aPython\u5e76\u6ca1\u6709\u8fd9\u4e2a\u9700\u8981\u3002\n\u6700\u597d\u7ed3\u5408\u4f7f\u7528\u4e00\u4e2a\u6307\u9488\u548c\u957f\u5ea6\u503c\u6765\u5904\u7406\u5b57\u7b26\u4e32\u3002\n\u4e0d\u8fc7\uff0c\u6709\u65f6\u5019\u4f60\u5fc5\u987b\u53bb\u5904\u7406C\u8bed\u8a00\u9057\u7559\u4ee3\u7801\u65f6\u5c31\u6ca1\u5f97\u9009\u62e9\u4e86\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5c3d\u7ba1\u5f88\u5bb9\u6613\u4f7f\u7528\uff0c\u4f46\u662f\u5f88\u5bb9\u6613\u5ffd\u89c6\u7684\u4e00\u4e2a\u95ee\u9898\u662f\u5728 PyArg_ParseTuple()\n\u4e2d\u4f7f\u7528\u201cs\u201d\u683c\u5f0f\u5316\u7801\u4f1a\u6709\u5185\u5b58\u635f\u8017\u3002\n\u4f46\u4f60\u9700\u8981\u4f7f\u7528\u8fd9\u79cd\u8f6c\u6362\u7684\u65f6\u5019\uff0c\u4e00\u4e2aUTF-8\u5b57\u7b26\u4e32\u88ab\u521b\u5efa\u5e76\u6c38\u4e45\u9644\u52a0\u5728\u539f\u59cb\u5b57\u7b26\u4e32\u5bf9\u8c61\u4e0a\u9762\u3002\n\u5982\u679c\u539f\u59cb\u5b57\u7b26\u4e32\u5305\u542b\u975eASCII\u5b57\u7b26\u7684\u8bdd\uff0c\u5c31\u4f1a\u5bfc\u81f4\u5b57\u7b26\u4e32\u7684\u5c3a\u5bf8\u589e\u5230\u4e00\u76f4\u5230\u88ab\u5783\u573e\u56de\u6536\u3002\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import sys\ns = 'Spicy Jalape\\u00f1o'\nsys.getsizeof(s)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "print_chars(s)     # Passing string"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "sys.getsizeof(s)   # Notice increased size"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u4f60\u5728\u4e4e\u8fd9\u4e2a\u5185\u5b58\u7684\u635f\u8017\uff0c\u4f60\u6700\u597d\u91cd\u5199\u4f60\u7684C\u6269\u5c55\u4ee3\u7801\uff0c\u8ba9\u5b83\u4f7f\u7528 PyUnicode_AsUTF8String() \u51fd\u6570\u3002\u5982\u4e0b\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "static PyObject *py_print_chars(PyObject *self, PyObject *args) {\n  PyObject *o, *bytes;\n  char *s;\n\n  if (!PyArg_ParseTuple(args, \"U\", &o)) {\n    return NULL;\n  }\n  bytes = PyUnicode_AsUTF8String(o);\n  s = PyBytes_AsString(bytes);\n  print_chars(s);\n  Py_DECREF(bytes);\n  Py_RETURN_NONE;\n}"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u901a\u8fc7\u8fd9\u4e2a\u4fee\u6539\uff0c\u4e00\u4e2aUTF-8\u7f16\u7801\u7684\u5b57\u7b26\u4e32\u6839\u636e\u9700\u8981\u88ab\u521b\u5efa\uff0c\u7136\u540e\u5728\u4f7f\u7528\u8fc7\u540e\u88ab\u4e22\u5f03\u3002\u4e0b\u9762\u662f\u4fee\u8ba2\u540e\u7684\u6548\u679c\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import sys\ns = 'Spicy Jalape\\u00f1o'\nsys.getsizeof(s)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "print_chars(s)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "sys.getsizeof(s)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u4f60\u8bd5\u7740\u4f20\u9012NULL\u7ed3\u5c3e\u5b57\u7b26\u4e32\u7ed9ctypes\u5305\u88c5\u8fc7\u7684\u51fd\u6570\uff0c\n\u8981\u6ce8\u610f\u7684\u662fctypes\u53ea\u80fd\u5141\u8bb8\u4f20\u9012\u5b57\u8282\uff0c\u5e76\u4e14\u5b83\u4e0d\u4f1a\u68c0\u67e5\u4e2d\u95f4\u5d4c\u5165\u7684NULL\u5b57\u8282\u3002\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import ctypes\nlib = ctypes.cdll.LoadLibrary(\"./libsample.so\")\nprint_chars = lib.print_chars\nprint_chars.argtypes = (ctypes.c_char_p,)\nprint_chars(b'Hello World')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "print_chars(b'Hello\\x00World')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "print_chars('Hello World')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u4f60\u60f3\u4f20\u9012\u5b57\u7b26\u4e32\u800c\u4e0d\u662f\u5b57\u8282\uff0c\u4f60\u9700\u8981\u5148\u6267\u884c\u624b\u52a8\u7684UTF-8\u7f16\u7801\u3002\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "print_chars('Hello World'.encode('utf-8'))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5bf9\u4e8e\u5176\u4ed6\u6269\u5c55\u5de5\u5177\uff08\u6bd4\u5982Swig\u3001Cython\uff09\uff0c\n\u5728\u4f60\u4f7f\u7528\u5b83\u4eec\u4f20\u9012\u5b57\u7b26\u4e32\u7ed9C\u4ee3\u7801\u65f6\u8981\u5148\u597d\u597d\u5b66\u4e60\u76f8\u5e94\u7684\u4e1c\u897f\u4e86\u3002"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.1"
    },
    "toc": {
      "base_numbering": 1,
      "nav_menu": {},
      "number_sections": true,
      "sideBar": true,
      "skip_h1_title": true,
      "title_cell": "Table of Contents",
      "title_sidebar": "Contents",
      "toc_cell": false,
      "toc_position": {},
      "toc_section_display": true,
      "toc_window_display": true
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}